home *** CD-ROM | disk | FTP | other *** search
/ Amiga Plus Leser 15 / Amiga Plus Leser CD 15.iso / Tools / Development / AmigaTalk_X / general / List.st < prev    next >
Encoding:
Text File  |  2002-03-13  |  2.3 KB  |  110 lines

  1. "-------------------------------------------------------"
  2. "  Lists are implemented using Points in order to       "
  3. "  reduce the number of classes in the standard prelude "
  4. "-------------------------------------------------------"
  5.  
  6. Class List :SequenceableCollection
  7. ! first current !
  8. [
  9.    add: anItem
  10.       first <- (Point new x: anItem ) y: first.
  11.       ^ anItem
  12. |
  13.    addFirst: anItem
  14.       first <- (Point new x: anItem ) y: first.
  15.       ^ anItem
  16. |
  17.    addLast: anItem
  18.       (first isNil) 
  19.          ifTrue: [^ self addFirst: anItem].
  20.  
  21.       (self findLast) y: ((Point new x: anItem) y: nil).
  22.  
  23.       ^ anItem
  24. |
  25.    addAllFirst: aCollection
  26.       aCollection do: [:x | self addFirst: x]
  27. |   
  28.    addAllLast: aCollection
  29.       aCollection do: [:x | self addLast: x]
  30. |
  31.    coerce: aCollection    ! newList !
  32.       newList <- List new.
  33.  
  34.       aCollection do: [:x | newList addLast: x].
  35.  
  36.       ^ newList
  37. |
  38.    findLast     ! item !
  39.       ((item <- first) isNil)
  40.          ifTrue: [^ nil].
  41.  
  42.       [(item y) notNil]
  43.          whileTrue: [item <- item y].
  44.  
  45.       ^ item
  46. |
  47.    remove: anItem
  48.       ^ self remove: anItem 
  49.            ifAbsent: [self error: 'cant find item']
  50. |
  51.    remove: anItem ifAbsent: exceptionBlock
  52.       (first isNil) 
  53.          ifTrue: [^ exceptionBlock value].
  54.  
  55.       self inject: nil 
  56.              into: [:prev :current |
  57.  
  58.                      (current x == anItem)
  59.                      ifTrue: [(prev isNil)
  60.                               ifTrue:  [first <- current y]
  61.                               ifFalse: [prev y: (current y)].
  62.          
  63.                               ^ anItem
  64.                              ].
  65.       
  66.                      current 
  67.                    ].
  68.       
  69.       ^ exceptionBlock value
  70. |
  71.    removeError
  72.       ^ self error: 'cannot remove from an empty list'
  73. |
  74.    removeFirst      ! item !
  75.       (first isNil)
  76.          ifTrue: [^ self removeError].
  77.  
  78.       item  <- first.
  79.       first <- first y.
  80.  
  81.       ^ item x
  82. |
  83.    removeLast
  84.       (first isNil)
  85.          ifTrue: [^ self removeError].
  86.  
  87.       ^ self remove: self last 
  88.            ifAbsent: [self removeError]
  89. |
  90.    first
  91.       ^ ((current <- first) notNil) 
  92.          ifTrue: [ current x ]
  93. |
  94.    next
  95.       ^ ((current <- current y) notNil)
  96.          ifTrue: [ current x ]
  97. |
  98.    current
  99.       ^ current x
  100. |
  101.    last
  102.       (first isNil) 
  103.          ifTrue: [^ nil].
  104.   
  105.       ^ self findLast x
  106. |
  107.    isEmpty
  108.       ^ first == nil
  109. ]
  110.